$(function(){

	jQuery.ajax({
		url: 'menu.action?command=getRoleMenuTree',
		type: "post",
		cache: false,
		dataType: "json",
		success: function(result){
			initNavMenu(result);
		}
	});

	$('#editPswForm').form({
		onSubmit: function(){
			if($(this).form('validate')){
				ajaxEditPsw();
			}else{
				return false;
			}
		}
	});

	$('#main-center').tabs({
		onClose: function(){
			$('#main-center').tabs("select", "欢迎使用");
		}	
	});
	
});

function editPsw(){
	$('#editPswWind').show();
	$('#editPswWind').window({
		closed: false,
		title: '修改密码'
	});
}

var lastTab = null;// 上一tab标签
function initNavMenu(jsonData) {
    var menulist = "";
	/// menulist += '<div class="easyui-accordion" fit="true" border="false">';
	menulist = $('<div class="easyui-accordion" fit="true" border="false"></div>');
	
	for(var i = 0; i < jsonData.length; i++){
		var n = jsonData[i];
		var menuDiv = $('<div title="'+n.text+'" icon="'+n.icon+'"></div>');
		if(i=='0'){
			///menulist += '<div selected="true" title="'+n.text+'" icon="'+n.icon+'"><ul>';
			menuDiv.attr("selected", true);
		}
		var menuUl = $('<ul></ul>');
		if(n.children){
			for(var j = 0; j < n.children.length; j++){
				var o = n.children[j];
				// update by cainiao 2011-10-06
				var menuIcon = $('<div class="'+o.icon+'">' + o.text + '</div>');
				var menuHref = $('<a target="mainFrame" href="javascript:void(0)" icon="'+ o.icon +'"></a>');
				menuHref.append(menuIcon);
				menuHref.attr("url", o.url);
				// 兼容IE6
				menuHref.click(function(){
					//$("#navHrefCtl").attr("href", $(this).attr("url"));
					//$("#navHrefCtl")[0].click();
					var tabTitle = $(this).text();
					var url = $(this).attr("url");//update by cainiao (href --> url: 防止拖动链接)
					var icon = $(this).attr("icon");
					
					if(lastTab != tabTitle){// 关闭上一tab标签
						$('#main-center').tabs('close', lastTab);
						addTab(tabTitle, url, icon);
					}else{// 2011-12-20
						$('#main-center').tabs('close', lastTab);
						addTab(tabTitle, url, icon);
						//$('#main-center').tabs('select', tabTitle);
					}
					lastTab = tabTitle;
	
					$(this).attr("href", $(this).attr("url"));
					$(this).attr("target", "mainFrame");
					// 火狐浏览器
					if(explorer() == 2){
						$(this).attr("href", "javascript:void(0)");
					}
	    		});
				///menulist += '<li><a target="mainFrame" href="' + o.url + '" icon="'+ o.icon +'"><div class="'+o.icon+'">' + o.text + '</div></a></li>';
				var menuLi = $('<li></li>');
				menuLi.append(menuHref);
				menuUl.append(menuLi);
			}
		}
		///menulist += '</ul></div>';
		menuDiv.append(menuUl);
		menulist.append(menuDiv);
	}
	///menulist += '</div>';
	
	///$("#navMenu").html(menulist);
	$("#navMenu").append(menulist);
	
	/*
	$('.easyui-accordion li a').click(function(){
		var tabTitle = $(this).text();
		var url = $(this).attr("url");//update by cainiao (href --> url: 防止拖动链接)
		var icon = $(this).attr("icon");
		
		if(lastTab != tabTitle){// 关闭上一tab标签
			$('#main-center').tabs('close', lastTab);
			addTab(tabTitle, url, icon);
		}
		lastTab = tabTitle;
	});
	*/

	$(".easyui-accordion").accordion({
		width: 180
	});
}

function addTab(subtitle, url, icon){
	$('#main-center').tabs('add',{
		icon: icon,
		title: subtitle,
		// href: url,
		content: createFrame(url),
		closable: true,
		width: $('#mainPanle').width()-10,
		height: $('#mainPanle').height()-26
	});
}

function createFrame(url){
	var s = '<iframe name="mainFrame" scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
	return s;
}

function loginOut(){
	if(lastTab){// 关闭上一tab标签
		//$('#main-center').tabs('close', lastTab);
	}
	$.messager.confirm('系统提示', '您确定要重新登录吗?', function(r) {
		if(r){
			location.href = "user.action?command=loginOut";
		}
	});
}

function explorer(){
	var flag = 0;
	if($.browser.msie) {
        flag = 0;// IE浏览器
	}else if($.browser.opera) {
		flag = 1;// opera浏览器
	}else if($.browser.mozilla) {
	    flag = 2;// firefox浏览器
	}else if($.browser.safari) {
		 flag = 3;// safari浏览器
	}
	return flag;
}

function exitSystem(){
	if(lastTab){// 关闭上一tab标签
	//$('#main-center').tabs('close', lastTab);
	}
	$.messager.confirm('系统提示', '您确定要退出本次登录吗?', function(r) {
		if(r){
			if(explorer() == 0){
				window.opener = "meizz";
			}
			if(explorer() == 2){
				window.open('', '_parent', '');
			}
			window.close();
		}
	});
}

function ajaxEditPsw(){
	var actionURL = "user.action?command=editPsw";
	var params = $("#editPswForm").serialize();
	
	jQuery.ajax({
		url: actionURL,
		type: "post",
		cache: false,
		data: params,
		success: function(result){
			if("true" == result){
				$.messager.alert("友情提示", "您的操作成功!", "info", function(){
					$('#editPswWind').window('close');
				});
			}else if("false" == result){
				$.messager.alert("友情提示", "您的操作失败!", "error");
			}else{
				$.messager.alert("友情提示", result, "error");
			}
		}
	});
}
